#Current thinks two sets of source lists
# one for main program and one for runtestmain

## make file for use with UnitTest++ Framework.
# use automatic variables
#	$@ is the target
#	$< is the first prerequiste
# 	$^ all the prerequistes

location = school
# NO spaces in Directory Names
# Moving toward all paths first
ifeq ($(location),school)
	UTBaseDir = "C:\Progra~1\unittest-cpp\UnitTest++"
	MLroot=C:\MathLink\DeveloperKit\Windows\CompilerAdditions\mldev32
endif
ifeq ($(location),home)
	UTBaseDir = "C:\unit_test_trials\unittestplusplus\unittest-cpp\UnitTest++"
	MLroot="c:\Progra~1\Wolfra~1\Mathematica\6.0\SystemFiles\Links\MathLink\DeveloperKit\Windows\CompilerAdditions\mldev32"
endif
addincdir := $(UTBaseDir)\src 

#MLroot=C:\mathlink\CompilerAdditions\mldev32
addincdir += $(MLroot)/include
VISAroot := C:\PROGRA~1\IVIFOU~1\VISA\WinNT
addincdir += $(VISAroot)/include
NIDAQroot=c:\Progra~1\Nation~1\NI-DAQ\DAQMXA~1
addincdir += $(NIDAQroot)/include

ifeq ($(findstring $(version),MinGW MinGW421),)  # nonzero if found
	addlibdir +=$(MLroot)/lib
	addlibdir +=$(VISAroot)/lib/msc
	addlibdir +=$(NIDAQroot)/lib/msvc	
	addlibdir +=$(UTBaseDir)\debug
	addlibs += ml32i3m
	addlibs += visa32
	addlibs += NIDAQmx
	addlibs += UnitTest++.vsnet2005
else
	addlibs += $(MLroot)/lib/ml32i3m.lib
	addlibs += $(VISAroot)/lib/msc/visa32.lib
	addlibs += $(NIDAQroot)/lib/msvc/NIDAQmx.lib
	addlibdir += $(UTBaseDir)
	addlibs += -lUnitTest++
endif

outdir := ..\build_$(version)\debug
testoutdir := tests\buildtests_$(version)
# all the sources except the one containing main.exe (i.e. vector_main.cpp)
# could use conditional include instead of omitting entire file
sources := getvoltages.cpp option.cpp getvoltages_helper.cpp job.cpp visaserial.cpp

# need the paths to be able to specify were objects are built
testsources = maintest.cpp
testsources += $(patsubst tests/test%,test%,$(wildcard tests/test*.cpp))
#~ tmp:
	#~ @echo $(testsources)
objects = $(patsubst %.cpp,$(outdir)/%.$(objext),$(sources))
testobjects = $(patsubst %.cpp,$(testoutdir)/%.$(objext),$(testsources))

include setup_windows_app.mak

# need to add directories for object files or use vpath or VPATH
VPATH = $(outdir) tests $(testoutdir)

all: $(outdir)/getvoltagestm.exe $(testoutdir)/maintest.exe run_tests 

-include $(outdir)/vector.d				# use g++ -MM > to generate depends file

myecho = echo
$(outdir)/%.exe: $(outdir)/%.$(objext) $(objects)
	$(myecho) rule build main executable
	@$(cc) $^ $(exeout)$@ $(linkopts) $(libdir) $(libraries)

$(testoutdir)/%.exe: $(testobjects) $(outdir)/getvoltagestm.$(objext) $(objects)
	$(myecho) rule build test main executable
	$(cc) $^ $(exeout)$@ $(linkopts) $(libdir) $(testlibraries)

$(testoutdir)/%.$(objext) : %.cpp
	$(myecho) rule build tests: $^
	@$(cc) $(testcopts) $(incdir) $(UTIncDir) $< $(objout)$@

$(outdir)/%.$(objext): %.cpp
	$(myecho) rule build src: $^
	@$(cc) $(copts) $(incdir) $< $(objout)$@

%tm.cpp : %.tm
	@$(MLroot)\bin\mprep $< -o $@

# depends should be run when files or includes changes
dinc := $(patsubst /I%,-I%,$(incdir))
depends_old:
	@echo use depends, depends_old has line length problems
#~ 	@echo building dependency file
#~ 	@echo # building dependency file 1 > tempDep1.txt
#~ 	@echo $(foreach file,$(wildcard *.cpp),"$(outdir)/$(patsubst %.o:,%.$(objext):,$(shell g++ $(dinc) -MM $(file)))"; >> tempDep1.txt)
#~ 	@echo $(foreach file,$(wildcard tests/*.cpp),"$(testoutdir)/$(patsubst %.o:,%.$(objext):,$(shell g++ $(dinc) -MM $(file)))"; >> tempDep1.txt)
#~ 	python utils\depUtil.py tempDep1.txt $(outdir)\vector.d
#	$(eval dd:=$(patsubst \,,$(dd)))
#	@utils\depends_util.bat $(dd)
#	@type $(outdir)\vector.d

depends:
	@python utils\depUtilFull.py $(outdir) $(dinc)
	
.PHONY : clean
clean:
	del /Q $(outdir)\*.*
	del /Q $(testoutdir)\*.*

run_tests:
	@echo *************************
	@$(testoutdir)\maintest.exe
	@echo *************************
#	@utils\color.bat
	
